//
//  SLMainViewController.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/19.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit
import SVProgressHUD

class SLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.添加子控制器
        addChildViewControllers()
       
        //2.然而我们只想用代码来替换现UITabBarController里面的tabBar.这时候可以用 KVC 来进行
        let newTabBar = SLTabBar()
        setValue(newTabBar, forKey: "tabBar")
        
        //3.准备闭包
        newTabBar.commposeClosure = { [weak self] () -> () in
            //判断用户登录
            if !SLUserAccountViewModel.sharedInstance.userLogin {
                SVProgressHUD.showErrorWithStatus("没有登录不能发微博")
                return
            }
            //根据字符串来创建类.Swift中通过类型来创建对象需要添加  产品名称.类名
//            let productName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? String ?? ""
//            let cls = NSClassFromString("\(productName).SLComposeViewController") as! UIViewController.Type
//            let composeVC = cls.init()
            let composeVC = SLComposeViewController()
            self!.presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
        }
     }

    //MARK: - 添加子控制器
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        
        let homeTableVC = SLHomeViewController()
        addChildViewController(homeTableVC, title: "首页", imageName: "tabbar_home")
        
        let messagerTableVC = SLMessageViewController()
        addChildViewController(messagerTableVC, title: "消息", imageName: "tabbar_message_center")
        
        let discoverTableVC = SLDiscoverViewConroller()
        addChildViewController(discoverTableVC, title: "发现", imageName: "tabbar_discover")
        
        let profileTableVC = SLProfileViewController()
        addChildViewController(profileTableVC, title: "我", imageName: "tabbar_profile")
    }
    
    /**
     添加tabBar子控制器
     
     :param: Controller 控制器
     :param: title      标题
     :param: imageName  图片名称
     */
     func addChildViewController(Controller: UIViewController, title: String, imageName: String) {
        Controller.title = title //设置标题
        Controller.tabBarItem.image = UIImage(named: imageName) //设置普通图片
        let  heightImageName = imageName + "_highlighted"//拼接图片名
        
        Controller.tabBarItem.selectedImage = UIImage(named: heightImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) //设置高亮图片
        
        Controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected) //设置title文字颜色
        
        addChildViewController(UINavigationController(rootViewController: Controller))
    }
}
